﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net.NetworkInformation;//ping类
using System.Net.Security;
using System.Net.Sockets;
using System.IO;
using System.Web;
using System.Web.Services;
using System.Net;
using System.IO.Compression;
using System.Windows.Forms;

namespace LPNRTool.ParkLock
{
    class LockMain
    {
        WriteLog Trace = new WriteLog();
        ReadJson ReadJsonToMain = new ReadJson();
     
        public Thread LockRunStartThread;
        public static int ControlLockBit = 0;
        public string RiseFallBit = "0";
        public static bool _isRiseFallExit = false;


        //public int SelectStateBit = 0;
        public static bool _isSelectStateExit = false;

        public int SetAutoBit = 0;
        public static bool _isAutoModeExit = false;
        public bool ModeStatus = false;

        public static bool _isSameTimeExit = false;

        private string slaveID;
        public string SlaveID
        {
            set { slaveID = value; }
        }

        private string slaveIdDValues;
        public string SlaveIDValues
        {
            get { return slaveIdDValues; }
            set { slaveIdDValues = value; }
        }

        private string bindNO;
        public string BindNO
        {
            set { bindNO = value; }
        }

        private string bindnoValues;
        public string BindNOValues
        {
            get { return bindnoValues; }
            set { bindnoValues = value; }
        }

        //测试记录
        private string strValue;
        public string StrValue
        {
            set
            {
                strValue = value;
            }
        }


        public void FallParkLock()
        {
            ControlLockBit = 1;
            _isRiseFallExit = false;
            RiseFallBit = "1";
            ThreadStart fallingThread = new ThreadStart(LockRun);
            LockRunStartThread = new Thread(fallingThread);
            LockRunStartThread.Start();
        }

        public void LockRun()
        {
            string url = "http://112.81.110.147:4520/parking-carlock-server/api/carLockApi/operate";//20170616更新url 外网地址

            string SlaveID = "slaveId";
            string BindNO = "bindNo";

            string RiseFall = "";
            //SelectDevice();//选择设备

            SlaveIDValues = "002017112017"; 
            BindNOValues = "948872";

            if (RiseFallBit == "1")
            {
                RiseFall = "降下车锁";//降落
            }
            else if (RiseFallBit == "2")
            {
                RiseFall = "升上车锁";//升
            }

            try
            {
                Trace.WriteLogFile("控制车锁地址" + url + "操作为" + RiseFall);
                Encoding encoding = Encoding.GetEncoding("utf-8");

                IDictionary<string, string> parameters = new Dictionary<string, string>();
                //bind test
                parameters.Add(SlaveID, SlaveIDValues);
                parameters.Add(BindNO, BindNOValues);

                parameters.Add("action", RiseFallBit);

                while (_isRiseFallExit == false)
                {
                    HttpWebResponse response = HTTPPost.CreatePostHttpResponse(url, parameters, encoding);

                    Stream stream = response.GetResponseStream();   //获取响应的字符串流
                    StreamReader sr = new StreamReader(stream);     //创建一个stream读取流
                    string receivehtml = sr.ReadToEnd();            //从头读到尾，放到字符串html
                    Trace.WriteLogFile(receivehtml);
                    ReadJsonToMain.SignalJsonMy(receivehtml);
                }
                ControlLockBit = 0;
            }
            catch (Exception ex)
            {
                Trace.WriteLogFile("操作失败"+ex.Message.ToString());
            }
        }

    }
}
